package com.example.a2032.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;


public class AxiesView extends View {
    Paint mPaint;
    public AxiesView(Context context) {
        super(context);
        init(context);
    }

    public AxiesView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public AxiesView(Context context, @Nullable  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        mPaint=new Paint();
        mPaint.setColor(Color.GREEN);
        mPaint.setStrokeWidth(1);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawAxes(canvas,mPaint);
    }

    private static final String TAG = "AxiesView";

    private  void drawAxes( Canvas canvas, Paint paint) {
        Log.i(TAG, "drawAxes: bg");
        Path pathCanvas=new Path();
        float xCount=12;
        float width=getWidth()/xCount;
        //画网格竖线
        float bottom=getHeight();
        float top=0;

        paint.setAlpha(200);

        float offsetY=bottom-top;
        int countY= (int) (offsetY/width)-paddingCount;
        for (int i = 0; i <xCount; i++) {
          //  pathCanvas.moveTo(i * width , (countY-1)*width);
            pathCanvas.moveTo(i * width , (countY)*width);
            pathCanvas.lineTo(i * width , top);
        }
        //画网格横线
        float end=getWidth();
        for (int i = 0; i <=countY; i++) {
            pathCanvas.moveTo(0, i * width + top);
            pathCanvas.lineTo(end, i * width + top);
        }
        canvas.drawPath(pathCanvas, paint);
        pathCanvas.reset();

    }
    int paddingCount=0;
    public void setPaddingCount(int paddingCount){
        this.paddingCount=paddingCount;
    }
}
